package com.member.web.job;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.enums.ActivityStatusEnum;
import com.member.common.enums.OrderDeleteEnum;
import com.member.shop.domain.SysBizActivity;
import com.member.shop.mapper.SysBizActivityMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时任务(活动报名截至)
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-11-30
 **/
@Component
@Log4j2
public class ActivityJob {

    @Resource
    private SysBizActivityMapper sysBizActivityMapper;

    /**
     * 从1日开始,每1天执行一次
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void sync() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = new Date();
        //转换为字符串
        String d1Str = df.format(d1);

        LambdaQueryWrapper<SysBizActivity> sysBizActivityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizActivityLambdaQueryWrapper.eq(SysBizActivity::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        List<SysBizActivity> sysBizActivities = sysBizActivityMapper.selectList(sysBizActivityLambdaQueryWrapper);
        for (SysBizActivity sysBizActivity : sysBizActivities) {
            String startTime = df.format(sysBizActivity.getActivityStartTime());
            String endTime = df.format(sysBizActivity.getActivityEndTime());
            if (d1Str.compareTo(startTime) < 0) {
                sysBizActivity.setStatus(ActivityStatusEnum.BEGIN.getKey());
            } else if (d1Str.compareTo(startTime) >= 0 && d1Str.compareTo(endTime) < 0) {
                sysBizActivity.setStatus(ActivityStatusEnum.ACTION.getKey());
            } else if (d1Str.compareTo(endTime) >= 0) {
                sysBizActivity.setStatus(ActivityStatusEnum.END.getKey());
            }
            sysBizActivityMapper.updateById(sysBizActivity);
        }
    }

}
